import {
  getVideoList,
  editVideoList
} from '@/api/video'

const state = {
  videoData: [],
  videoDataCount: 0
}

const mutations = {
  GET_VIDEO_DATA: (state, data) => {
    state.videoData = data
  },
  GET_VIDEO_DATA_COUNT: (state, data) => {
    state.videoDataCount = data
  }
}

const actions = {
  async getVideoData({ commit }, playload) {
    let data = {}
    let newData = []
    if (Object.keys(playload).length > 0) {
      data = playload
    }
    const resp = await getVideoList(data)
    const host = resp.data.host
    resp.data.data.rows.map((item) => {
      newData = [...newData, {
        id: item.id,
        host: host,
        title: item.title,
        img: item.img,
        url: item.url,
        state: item.state
      }]
    })
    // for (let i = 0; i < 5; i++) {
    //   newData = [...newData, {
    //     id: i + 1,
    //     host: 'http://static.baisha.work',
    //     vname: '法拉利',
    //     link: 'www.shipin.com',
    //     ctime: '2019-04-24 14:58:00',
    //     state: 0,
    //     img: '/upload/car/20190506/s653.jpg'
    //   }]
    // }
    commit('GET_VIDEO_DATA', newData)
    commit('GET_VIDEO_DATA_COUNT', resp.data.data.total)
    return resp
  },
  async editVideoData({ commit }, playload) {
    const resp = await editVideoList(playload)
    return resp
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
